package org.hxyjs.mq.round;

import org.hxyjs.mq.ConnectionUtils;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class SenderWork {
 
	private static final String QUEUE = "java015_queue";
 
	public static void main(String[] args) {
		Connection con = null;
		Channel channel = null;
		try {
			// 获取连接
			con = ConnectionUtils.getConnection();
			// 从连接中创建通道
			channel = con.createChannel();
			// 声明一个队列
			channel.queueDeclare(QUEUE, false, false, false, null);
			// 发送50条消息 
			for (int i = 0; i < 50; i++) {
				// 消息内容
				String msg = "work queue " + i;
				// 发送消息
				channel.basicPublish("", QUEUE, null, msg.getBytes());
				try {
					Thread.sleep(i * 20);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
			System.out.println("send success");
		} catch (IOException e) {
			e.printStackTrace();
		} catch (TimeoutException e) {
			e.printStackTrace();
		} finally {
			// 关闭连接
			ConnectionUtils.close(channel, con);
		}
	}
}